#include <stdio.h>
#include <stdlib.h>
#include <iomanip>
#include <string.h>
#include <iostream>
#include <queue>
#include <fstream>
#include <stdarg.h>
#include <sys/types.h>
#include <unistd.h>

using namespace std;

struct logger_t
{
    int edge;
    char* message;
    char* log_file;
};

ofstream fd;
queue<char*> q;


logger_t* init (int edge, char* file)
{
    logger_t* logger = (logger_t*)malloc(sizeof(logger_t));
    if (logger==NULL) {
        perror ("logger can't be created");
        return NULL;
    }
    logger->edge=edge;
    logger->log_file=file;
    return logger;
}

int add_mes(logger_t* logger, int level, const char* format, ...)
{
    char* buf=(char*)malloc(sizeof(char));
    int length;
    if (logger == NULL) {
        return 0;
    }
    if (level<logger->edge) return 0;
    va_list args;
    va_start(args, format);
    vsprintf(buf, format, args);
    va_end(args);
    q.push(buf);
    return 1;
}

void print(logger_t* logger)
{
    char* buf=(char*)malloc(sizeof(char));
    buf=q.front();
    q.pop();
    fd.open(logger->log_file, ios::app);
    fd<<"err:"<<buf<<"\n";
    fd.close();

}

void destroy(logger_t* logger)
{
    if (logger==NULL) return ;
    free(logger);
}


